/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary;

import entity.Event;
import entity.Notification;
import entity.Participant;
import entity.ResearchUser;
import entity.User;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import security.EventBean;


/**
 *
 * @author MONESI
 */

@Stateless
@Named
public class NotificationManager {
    
    @PersistenceContext
    EntityManager em;
    
   
      
    @Inject
    Principal principal;
          
    List<String> nonLette;
    List<Integer> n;
    
    @EJB
    UserFacade uf;
    
    
    public void save(Notification news) {
        
        int x;
        int n;
        List<Integer> i = em.createQuery("SELECT MAX(n.id) FROM NOTIFICATION n ").getResultList();
        
        x=i.size()-1;
        n=i.get(x);
        n++;
        news.setId(n);
        
        em.persist(news);
        
    }
    
    public List<String> notificheNonLette(String loggedUser) {
        
        nonLette = em.createQuery("SELECT n.contenuto FROM NOTIFICATION n WHERE n.destinatario LIKE :logged AND n.letta = 0").setParameter("logged", loggedUser).getResultList();                   
        
        return nonLette;        
    }
    
         
       
    public void azzera(String loggedUser) {
        
        Query q = em.createQuery("UPDATE NOTIFICATION n SET n.letta =1 WHERE n.destinatario LIKE :loggedUser").setParameter("loggedUser", loggedUser);
        q.executeUpdate();
          
    }
    
       
    public List<Integer> number(String loggedUser){
        
          n = em.createQuery("SELECT COUNT(n.id) FROM NOTIFICATION n WHERE n.destinatario LIKE :logged AND n.letta = 0").setParameter("logged", loggedUser).getResultList();
          return n;
    }
   
    
}

